﻿using System.Linq;
using KharaPos.Views;

namespace KharaPos
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using LoginUI;

    public partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();
            loginContainer.Child = new LoginStatus();
        }

        /// <summary>
        ///     After the Frame navigates, ensure the <see cref="HyperlinkButton"/> representing the current page is selected
        /// </summary>
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            foreach (var hb in
                LinksStackPanel.Children.Select(child => child as HyperlinkButton).Where(hb => hb != null && hb.NavigateUri != null))
            {
                VisualStateManager.GoToState(hb,
                                             hb.NavigateUri.ToString().Equals(e.Uri.ToString())
                                                 ? "ActiveLink"
                                                 : "InactiveLink", true);
            }
        }

        /// <summary>
        ///     If an error occurs during navigation, show an error window
        /// </summary>
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ErrorWindow.CreateNew(e.Exception);
        }
    }
}